﻿namespace Neo4jConnect.Test
{
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class RelationshipTest : TestBase
    {        
        public enum MyRelationshipTypes
        {
            Root,
            Depend
        }
     
        [TestMethod]
        public void CreateRelationship()
        {
            var graphDb = new GraphDatabaseService(Neo4jUri);
            Node node01 = graphDb.CreateNode();
            Node node02 = graphDb.CreateNode();
            Node node03 = graphDb.CreateNode();

            node01.Save();
            node02.Save();
            node03.Save();

            Relationship relationship01To02 = node01.CreateRelationshipTo(node02, MyRelationshipTypes.Depend);
            Relationship relationship02To03 = node02.CreateRelationshipTo(node03, MyRelationshipTypes.Depend);

            relationship01To02.SetProperty("key1", "val1");
            relationship01To02.SetProperty("key2", "val2");
            relationship01To02.Save();

            relationship02To03.SetProperty("key1", "val1");
            relationship02To03.SetProperty("key2", "val2");
            relationship02To03.Save();

            node01 = graphDb.GetNode(node01.Id);
            node02 = graphDb.GetNode(node02.Id);
            node03 = graphDb.GetNode(node03.Id);

            var node01Relationships = node01.GetRelationships();
            
            Assert.IsTrue(node01.GetRelationships().Count() == 1);
            Assert.IsTrue(node01.GetRelationships(MyRelationshipTypes.Depend).Count() == 1);
            Assert.IsTrue(node01.GetRelationships(RelationshipDirection.In).Count() == 0);
            Assert.IsTrue(node01.GetRelationships(RelationshipDirection.Out).Count() == 1);
                                    
            Assert.AreEqual("val1", node01Relationships.First().GetProperty<string>("key1"));            
        }
    }
}
